package com.jiyun.controller;

import com.jiyun.config.AliyunOSSUtils;
import com.jiyun.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;


@RestController
@RequestMapping("/api/imageUpload")
public class ImageUpload {

    @Autowired
    AliyunOSSUtils aliyunOSSUtils;

    @Autowired
    RedisTemplate redisTemplate;

//    @RequiresPermissions("imageUpload:imageUpload:files")
//    @Log(title = "设备档案", businessType = BusinessType.INSERT)
    @PostMapping("/files")
    public Result file(MultipartFile file) throws IOException {
        // 获取文件原始名称
        String originalFilename = file.getOriginalFilename();
        // 获取文件输入流
        InputStream inputStream = file.getInputStream();
        // 上传文件到阿里云OSS，并返回文件的URL
        String fileUrl = aliyunOSSUtils.putObject(inputStream, originalFilename);
        System.out.println(fileUrl);
        redisTemplate.opsForValue().set("fileUrl",fileUrl);
//        获取rabbit的数据
        // 返回上传成功的结果，包含文件URL
        return Result.success(fileUrl);
    }

}
